package com.cbd.basic.leetcode.easy;

import lombok.extern.slf4j.Slf4j;

/**
 * 合并两个有序数组
 */
@Slf4j
public class Solution88 {
    public static void main(String[] args) {
        int[] nums1 = new int[]{1,2,3,0,0,0};
        int[] nums2 = new int[]{2,5,6};
        merge(nums1, 3, nums2, 3);
        log.info("result = {}", nums1);
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m - 1;
        int p2 = n - 1;
        int tail = m + n - 1;
        int temp;
        while (p1 >= 0 || p2 >= 0) {
            if (p1 < 0) {
                temp = nums2[p2--];
            } else if (p2 < 0) {
                temp = nums1[p1--];
            } else if (nums1[p1] > nums2[p2]) {
                temp = nums1[p1--];
            } else {
                temp = nums2[p2--];
            }
            nums1[tail--] = temp;
        }
    }
}
